// pages/03setup/02printer/printerSet.js
var app = getApp()
var appData = app.globalData
Page({
   data: {
      printerList: [],
      typeArr: ['请选择', 'wifi', 'GPRS', '蓝牙', '网络', '其他'],
      isShowLabelArr: [0, 0, 0, 0, 0, 0, 0],
      mode: '',
      buttonText: '添加打印机',
      pick_i: 0,
      edit_id: -1
   },

   onLoad: function (options) {
      var that = this
      var param = { shop_id: appData.shopInfo.shopInfo.userid }
      app.getJsonData('api/admin/printer_list.php', param, function (res) {
         that.setData({ printerList: res.data })
      })
   },

   addButtonChange: function () {
      this.setData({
         pick_i: 0,
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消添加' : '添加打印机',
         edit_id: -1,
         isShowLabelArr: [0, 0, 0, 0, 0, 0, 0],
      })
   },

   editPrinter: function (e) {
      var td = this.data
      var index = e.currentTarget.id
      var printer = td.printerList[index]
      this.setData({
         edit_id: td.edit_id == index ? -1 : index,
         isShowLabelArr: index > -1 ? [1, 1, 1, 1, 1, 1, 1] : [0, 0, 0, 0, 0, 0, 0],
         pick_i: printer.type,
         mode: '',
         buttonText: '添加打印机',
      })
      console.log('this.data==', td)
   },

   submitPrinter: function (e) {
      var t = this
      var td = this.data
      var param = e.detail.value
      console.log('td==', td)
      if (td.edit_id > 0) {
         param.printer_id = td.printerList[td.edit_id].printer_id
      }
      param.shop_id = appData.shopInfo.userid
      console.log('e==', e, 'param==', param)
      app.getJsonData('api/admin/set_printer.php', param, function (res) {
         if (td.edit_id == -1) {
            t.addButtonChange()
         } else {
            t.setData({ edit_id: -1 })
         }
         t.setData({ isShowLabelArr: [0, 0, 0, 0, 0, 0, 0] })
         t.onLoad()
      })
   },

   showLabel: function (e) {
      var val = e.detail.value
      if (val.length > 1) return
      var arr = this.data.isShowLabelArr
      var i = e.currentTarget.id

      if (i == 0) {//0表示是picker,val值范围是
         this.setData({ pick_i: val })
         arr[i] = val == 0 ? 0 : 1
      } else {
         arr[i] = val == '' ? 0 : 1
      }

      this.setData({ isShowLabelArr: arr })
   },
})